package com.github.ruangnazi.sharing.master

import android.content.*
import android.net.Uri
import android.util.Log
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


data class PlatformData(
    val name :String,
    val pkg : String,
    val component: String,
    var enable: Boolean,
)

class ShardingSystem{
    var photoPlatform: Map<String, PlatformData> = mapOf<String,PlatformData>(
//        "bili" to PlatformData("bili","tv.danmaku.bili","",  true),
//        "douban" to PlatformData("douban","com.douban.frodo", "", true),
        "weibo" to PlatformData("weibo","com.sina.weibo", "", true),
        "qzone" to PlatformData("qzone","com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity", true),
//        "mm" to PlatformData("mm","com.tencent.mm","com.tencent.mm.ui.tools.ShareToTimeLineUI",  true),
   )
    val fragment: Fragment

    constructor(fg: Fragment){
        fragment = fg
        for ((k,v) in photoPlatform){
            var enable: Boolean by SharedPreferencesUtil(fragment.requireContext(), "photo",k, true)
            v.enable = enable
        }
    }

    fun ChangePhotoStatus(name:String,status:Boolean){
        for ((k,v) in photoPlatform){
            if (k == name){
                v.enable = status
                var enable: Boolean by SharedPreferencesUtil(fragment.requireContext(), "photo",k, false)
                enable = status
                break
            }
        }
    }



    fun SharingPhoto( title:String, imageUris:ArrayList<Uri>,imagePaths:ArrayList<String>): MutableList<Intent>{
        var retIntent: MutableList<Intent> = mutableListOf()
        for ((k,v) in photoPlatform){
            Log.e("send",k + " "+v.enable.toString())
            if (v.enable){
                var  shareIntent =  Intent();
                shareIntent.action = Intent.ACTION_SEND_MULTIPLE;
//                shareIntent.putExtra(Intent.EXTRA_TEXT, title);

                shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
//                shareIntent.type = "image/*";
                shareIntent.type = "image/*";
                if (v.component != ""){
                    var comp =  ComponentName(v.pkg,v.component)
                    shareIntent.component = comp;
                }else{
                    shareIntent.setPackage(v.pkg)
                }
//                shareIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK;

//                val textArr: ArrayList<String> = ArrayList()
//                textArr.add(title)
//                shareIntent.putStringArrayListExtra(Intent.EXTRA_TEXT,textArr)
                if (k=="mm"){
                    shareIntent.putExtra("Kdescription", title);
                }else{
                    shareIntent.putExtra(Intent.EXTRA_TEXT, title);
                    shareIntent.putExtra(Intent.EXTRA_TITLE,"title")
//                    shareIntent.type = "text/plain";
                }
                shareIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION

//                fragment.startActivityForResult(Intent.createChooser(shareIntent, "sharing"),31)

                retIntent.add(shareIntent)
//                startActivity.launch(Intent.createChooser(shareIntent, "sharing"))
//                var intent =  Intent()
//                intent.setAction(Intent.ACTION_SEND);
//                intent.putExtra(Intent.EXTRA_TEXT, "文本内容");
//                intent.setType("text/plain");
//                fragment.startActivity(intent)

//                val sp = Platform.ShareParams()
//
//                sp.text = title
//                var ima = imagePaths.toTypedArray()
//                Log.e("img", imagePaths[0])
////                sp.imagePath = imagePaths[0]
////                sp.lcImage
//                sp.imageArray = ima
//
////                val tw = ShareSDK.getPlatform(SinaWeibo.NAME)
//                val tw = ShareSDK.getPlatform("Wechat")
//
//                tw.share(sp)
//                Log.e("send","send start aaaaa")
            }
        }
        return retIntent
    }
}


class SharedPreferencesUtil<T>(val context: Context,val ns:String="default" ,  val name:String, val default:T) : ReadWriteProperty<Any?, T> {
    val prefs: SharedPreferences by lazy {
        context.getSharedPreferences(ns, Context.MODE_PRIVATE)
    }


    override fun getValue(thisRef: Any?, property: KProperty<*>): T {
        return findPreference(name, default)
    }

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
        putPreference(name, value)
    }

    private fun findPreference(name:String, default: T) : T  = with(prefs){
        return when (default) {
            is Long -> getLong(name, default)
            is String -> getString(name, default)
            is Int -> getInt(name, default)
            is Boolean -> getBoolean(name, default)
            is Float -> getFloat(name, default)
            else -> throw IllegalArgumentException("Unsupport type")
        } as T
    }

    private fun <T> putPreference(name:String, value:T) = with(prefs.edit()) {
        when (value) {
            is Long -> putLong(name, value)
            is String -> putString(name, value)
            is Int -> putInt(name, value)
            is Boolean -> putBoolean(name, value)
            is Float -> putFloat(name, value)
            else -> throw IllegalArgumentException("Unsupport type")
        }.apply()
        //commit和apply都表示提交
        //应该是对when-else语句的返回值调用apply方法
    }

}